import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/model/FictionChapterData.dart';
import 'package:provider/provider.dart';

import 'FictionContentPageViewModel.dart';

class FictionContentPage extends StatefulWidget {
  FictionContentPage({Key key, @required this.chapter}) : super(key: key);

  final FictionChapterData chapter;

  @override
  State<StatefulWidget> createState() => FictionContentState(chapter);
}

class FictionContentState extends State<FictionContentPage> {
  FictionContentState(this.chapter);

  final FictionChapterData chapter;

  ///是否展示上/下一章
  var showNextButton = false;

  FictionContentPageViewModel viewModel = FictionContentPageViewModel();

  @override
  void initState() {
    super.initState();
    viewModel.getChapterContent(chapter.chapterId);
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      child: ChangeNotifierProvider(
        create: (_) => viewModel,
        child: Consumer<FictionContentPageViewModel>(
          builder: (_, model, __) => Scaffold(
              appBar: AppBar(
                title: Text(model.info == null ? "" : chapter.title),
              ),
              body: CustomScrollView(
                slivers: [
                  if(showNextButton)SliverToBoxAdapter(
                    child: Container(
                        margin: EdgeInsets.all(16),
                        child: Row(
                          children: [
                            Expanded(
                                child: MaterialButton(
                              onPressed: () {},
                              child: Text("上一章"),
                            )),
                            Expanded(
                              child: MaterialButton(
                                onPressed: () {},
                                child: Text("下一章"),
                              ),
                            )
                          ],
                        )),
                  ),
                  SliverList(
                    delegate: SliverChildBuilderDelegate(
                        (BuildContext context, int index) {
                      return Text(
                          model.info == null ? "" : model.info.content[index]);
                    },
                        childCount:
                            model.info == null ? 0 : model.info.content.length),
                  )
                ],
              )),
        ),
      ),
    );
  }
}
